package com.ja.openchat.viewmodels;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.ja.openchat.R;
import com.ja.openchat.adapters.node.section.ItemNode;
import com.ja.openchat.adapters.node.section.RootNode;
import com.ja.openchat.ui.me.MyAlbumFragment;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.zy.openchat.core.model.user.UserInfo;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;


public class DynamicDetailsViewModel extends BaseViewModel {
    public ObservableField<String> entity = new ObservableField<>();

    public ObservableField<UserInfo> userAvatar = new ObservableField<>(UserInfo.getUserInfo());
    List<Integer> datas = new ArrayList<>();

    public DynamicDetailsViewModel(@NonNull Application application) {
        super(application);
        this.entity.set("数据1");
        datas.add(R.mipmap.zdy_1);
        datas.add(R.mipmap.zdy_2);
        datas.add(R.mipmap.zdy_3);
        datas.add(R.mipmap.zdy_4);
        datas.add(R.mipmap.zdy_5);
    }

    //点击事件
    public BindingCommand btnOnClickCommand = new BindingCommand(() -> {
    });
    //点击事件
    public BindingCommand headerOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(MyAlbumFragment.class.getCanonicalName());
    });
    //评论点击事件
    public BindingCommand commentOnClickCommand = new BindingCommand(() -> {
        ToastUtils.showShort("点击评论" + entity.get());
    });
    //条目的点击事件
    public BindingCommand itemMoreClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ToastUtils.showShort("itemMoreClick" + entity.get());
        }
    });
    public BindingCommand<Banner> banner = new BindingCommand(new BindingConsumer<Banner>() {
        @Override
        public void call(Banner banner) {
            banner.setAdapter(new BannerImageAdapter<Integer>(datas) {
                @Override
                public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ToastUtils.showShort("点击图片" + position);
                        }
                    });
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                            .load(data).centerCrop()
                            .into(holder.imageView);
                }
            }).setIndicator(new CircleIndicator(getApplication()));

        }
    });

    public List<BaseNode> getEntityData() {
        List<BaseNode> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            //Item Node
            ItemNode itemEntity1 = new ItemNode(R.mipmap.zdy_bg, "Root " + i + " - SecondNode 0");
            ItemNode itemEntity2 = new ItemNode(R.mipmap.zdy_bg, "Root " + i + " - SecondNode 1");
            ItemNode itemEntity3 = new ItemNode(R.mipmap.zdy_bg, "Root " + i + " - SecondNode 2");
            ItemNode itemEntity4 = new ItemNode(R.mipmap.zdy_bg, "Root " + i + " - SecondNode 3");
            ItemNode itemEntity5 = new ItemNode(R.mipmap.zdy_bg, "Root " + i + " - SecondNode 4");
            List<BaseNode> items = new ArrayList<>();
            items.add(itemEntity1);
            items.add(itemEntity2);
            // Root Node
            RootNode entity = new RootNode(items, "Root Node " + i);
            if (i > 0) {
                // 第1号数据开始默认不展开
                entity.setExpanded(false);
                items.add(itemEntity1);
                items.add(itemEntity2);
                items.add(itemEntity3);
                items.add(itemEntity4);
                items.add(itemEntity5);
            }

            list.add(entity);
        }
        return list;
    }
}
